package NWish.properties.object3ds
{
	import NWish.Away3d;
	import NWish.properties.Properties;
	
	import away3d.containers.Scene3D;
	import away3d.containers.View3D;

	public class View3DProperties extends Properties
	{
		public function View3DProperties(parent)
		{
			super(parent);
			
			var keys
			= 
				['name'
				//,'width'
				//,'height'
				,'x'
				,'y'
				,'antiAlias'
				,'backgroundColor'
				,'backgroundAlpha'
				,'forceMouseMove'
				,new Filters3dProperties(parent)
				];
			addPropertieKeys('View',keys);
			
			
			var keysRead
			= 
				['deltaTime'
				,'renderedFacesCount'
				];
			addPropertieKeys_read('View',keysRead);
			
			
			//addPropertieKeys_save('View',['resources']);
		}
		
		private function get away3d()
		{
			return _app.getPlugin('Away3d');
		}
		
		override public function toString()
		{
			return 'View3D';
		}
		override public function get type()
		{
			return View3D;
		}
		override public function newType(scene,dict)
		{
			var v:View3D =  new type();
			v.camera.name = Away3d.SystemCamera;
			v.scene.extra = new Object();
			v.scene.extra.ambientColor = 0x555555;
			return v;
		}
		override public function get createEnable()
		{
			if(away3d.view)
				return false;
			
			return true;
		}
		
		override public function dictToProperties(d,obj,scene:Scene3D,isloading)
		{
			super.dictToProperties(d,obj,scene,isloading);
			
			if(obj.parent == null)
			{
				away3d.addView3D(obj);
			}
		}
		
		
		override public function load(dict,obj,scene,subPropertiesType:Array = null)
		{
			return super.load1(dict,obj,scene,subPropertiesType);
		}
	}
}


import NWish.json.OrderDict;
import NWish.properties.Properties;
import NWish.properties.object3ds.filters3d.*;

import away3d.containers.View3D;



class Filters3dProperties
{
	private var _filters3dnames =
		['BloomFilter3D'
		,'BlurFilter3D'
		,'DepthOfFieldFilter3D'
		,'MotionBlurFilter3D'
		];
	
	private var _filters3PropertiesList;
	
	public function Filters3dProperties(parent)
	{
		_filters3PropertiesList =
			[new BlurFilter3DProperties()
			,new MotionBlurFilter3DProperties()
			,new DepthOfFieldFilter3DProperties(parent)
			,new BloomFilter3DProperties()
			];
		
		
	}
	
	public function propertiesToDict(view:View3D,dict:OrderDict,read_only = false,saving = false)
	{
		if(read_only == false)
		{
			if(saving == false)
			{
				dict.push('add filters3d','');
				dict.push(Properties.readMark + 'filters3ds',_filters3dnames);
			}
			
			for each(var f in view.filters3d)
			{
				for each(var pt in _filters3PropertiesList)
				{
					if(f is pt.type)
					{
						var sub = new OrderDict();
						pt.propertiesToDict(f,sub,read_only,saving);
						dict.push(pt.toString(),sub);
					}
				}
			}
		}
		else
		{
			for each(var f in view.filters3d)
			{
				for each(var pt in _filters3PropertiesList)
				{
					if(f is pt.type)
					{
						var sub = dict.get(pt.toString());
						pt.propertiesToDict(f,sub,read_only,saving);
					}
				}
			}
		}
	}
	
	private function create(view,type)
	{
		if(view.filters3d == null)
		{
			view.filters3d = [new type()];
		}
		else
		{
			var fs = view.filters3d;
			fs.push(new type());
			view.filters3d = fs;
		}
		
	}
	public function dictToProperties(dict,view:View3D,scene,isloading)
	{
		var af = dict['add filters3d'];
		
		if(af && isloading == false)
		{
			for each(var fpt in _filters3PropertiesList)
			{
				if(fpt.toString() == af)
				{
					create(view,fpt.type);
					break;
				}
			}
		}
		else if(af == null && isloading)//loading
		{
			for each(var fpt in _filters3PropertiesList)
			{
				var sub = dict[fpt.toString()];
				if(sub)
				{
					create(view,fpt.type);
				}
			}
		}
		
		//
		for each(var fpt in _filters3PropertiesList)
		{
			var sub = dict[fpt.toString()];
			if(sub)
			{
				for each(var f in view.filters3d)
				{
					if(f is fpt.type)
					{
						fpt.dictToProperties(sub,f,scene,isloading)
					}
				}
			}
		}
		
	}
	
}




